<?php
/*
 * 微信模板消息.
 * User: 陈立强
 * Date: 2019/11/22
 * Time: 17:07
 */
namespace app\common\service;

use app\common\model\Members AS MembersModel;
use \weixin\jssdk;

class Wxtemplate extends Base

{
    /**
     * 微信模板消息
     *
     * $openid          string      用户openid
     * $template_id     string      模板ID
     * $content         array       发送内容
     *
     */
    public function _wxSendTemplateMsg($openid,$template_id,$content){
        $jssdk = new jssdk();
        $access_token = $jssdk->getAccessToken();
        $data = [
            'touser' => $openid,            //'oyD7s0qppYtkFZQP8z0E7DdNvYd4',
            'template_id' => $template_id,//'9auggtS8o-CFgO0ETJiGT9DzCRe9vz8UoRyZtUPaWm8',
            'url' => '',
            'data' => $content
        ];
        if(!empty($data)){
            $rt=$this->curlPost('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token,json_encode($data),0);
        }
    }

    public function curlPost($url, $data,$showError=1){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
        if(!empty($data)){
            curl_setopt($curl,CURLOPT_POST,1);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }

        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}